/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package org.apache.directory.studio.persistence.view.wizards;


import org.apache.directory.studio.persistence.core.EntryProperties;
import org.apache.directory.studio.persistence.core.JavaType;
import org.apache.directory.studio.persistence.core.Cardinality;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;


/**
 * TODO AttributeEditingSupport.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class AttributeEditingSupport extends EditingSupport
{
    private Composite parent;
    private CellEditor editor;
    private int column;
    private AddMembersWizardPage addMembersWizardPage;


    /**
     * Creates a new instance of AttributeEditingSupport.
     *
     * @param viewer
     */
    public AttributeEditingSupport( ColumnViewer viewer, int column, AddMembersWizardPage wizardPage )
    {
        super( viewer );
        this.column = column;
        this.addMembersWizardPage = wizardPage;
        this.parent = ( ( TableViewer ) viewer ).getTable();

        switch ( column )
        {
            case 1:
                editor = new TextCellEditor( parent );
                break;
            case 2:
                editor = new ComboBoxCellEditor( parent, JavaType.getStrings() );
                break;
            case 3:
                editor = new ComboBoxCellEditor( parent, Cardinality.getStrings() );
                break;
            default:
                editor = new TextCellEditor( parent );
                break;
        }
    }


    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
     */
    @Override
    protected boolean canEdit( Object element )
    {
        return true;
    }


    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.EditingSupport#getCellEditor(java.lang.Object)
     */
    @Override
    protected CellEditor getCellEditor( Object element )
    {
        return editor;
    }


    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
     */
    @Override
    protected Object getValue( Object element )
    {
        EntryProperties info = ( EntryProperties ) element;
        switch ( this.column )
        {
            case 0:
                return info.getLdapAttributeName();
            case 1:
                return info.getJavaMemberName();
            case 2:
                return info.getJavaType().ordinal();
            case 3:
                return info.getCardinality().ordinal();
            default:
                break;
        }
        return null;

    }


    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
     */
    @Override
    protected void setValue( Object element, Object value )
    {
        // TODO: Some code should be put here to verify that
        // the input of the user does not collide with an existing
        // java class member identifier.

        // TODO: Refuse the input from the user and do not update the properties
        // if the identifier is already used. 

        EntryProperties entryProperties = ( EntryProperties ) element;
        //entryProperties.setGen( isGen );
        switch ( this.column )
        {
            case 0:
                entryProperties.setLdapAttributeName( String.valueOf( value ) );
                //entryProperties.setGen( isGen );
                break;
            case 1:
                entryProperties.setJavaMemberName( String.valueOf( value ) );
                addMembersWizardPage.dialogChanged();
                break;
            case 2:
                Integer i = ( Integer ) value;
                JavaType javaType = JavaType.values()[i];
                entryProperties.setJavaType( javaType );
                break;
            case 3:
                Integer j = ( Integer ) value;
                Cardinality cardinality = Cardinality.values()[j];
                entryProperties.setCardinality( cardinality );
                break;
            default:
                break;
        }

        getViewer().update( element, null );
    }

}
